import Cookies from "js-cookie";

// initial state
const state = {
  device: "",
  routes: [],
  cachedViews: "",
  visitedViews: [],
  sidebar: {
    opened: Cookies.get("sidebarStatus") !== "closed",
    withoutAnimation: false
  }
};

// getters
const getters = {};

// actions
const actions = {
  ToggleSideBar: ({ commit }) => {
    commit("TOGGLE_SIDEBAR");
  },
  CloseSideBar: ({ commit }, withoutAnimation) => {
    commit("CLOSE_SIDEBAR", withoutAnimation);
    return withoutAnimation;
  },
  ToggleDevice: ({ commit }, device) => {
    commit("TOGGLE_DEVICE", device);
    return device;
  },
  LoadRouter: ({ commit }, ROUTES) => {
    commit("SET_ROUTES", ROUTES);
    return ROUTES;
  },
  delCachedView: ({ commit }, view) => {
    commit("ADD_CACHED_VIEW", view);
    return view;
  },
  addVisitedView: ({ commit }, view) => {
    commit("ADD_VISITED_VIEW", view);
    return view;
  },
  delVisitedView: ({ commit }, view) => {
    commit("DEL_VISITED_VIEW", view);
    return view;
  },
  delAllViews: ({ commit }, view) => {
    commit("DEL_ALL_VIEWS", view);
    return [];
  }

  /*   @Action({ commit: 'SET_ROUTES' })
  public async LoadRouter() {
    const rs = await AuthApi.loadRouterUsingGET();
    if (rs.status == 200) {
      return rs.payload;
    }
  } */
  /* tagview */
};

// mutations
const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      Cookies.set("sidebarStatus", 1);
    } else {
      Cookies.set("sidebarStatus", 0);
    }
  },

  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set("sidebarStatus", "closed");
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },

  SET_ROUTES: (state, routes) => {
    state.routes = routes;
  },

  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  /* tagsView */

  ADD_CACHED_VIEW: (state, v) => {
    if (state.cachedViews.includes(v.name)) return;
    // if (!view.meta.noCache) {
    state.cachedViews.push(v.name);
    // }
  },
  ADD_VISITED_VIEW: (state, view, cb) => {
    if (state.visitedViews.some(v => v.path === view.path)) return;
    state.visitedViews = state.visitedViews.filter(
      v => !v.matched[v.matched.length - 1].regex.test(view.fullPath)
    );
    state.visitedViews.push(
      Object.assign({}, view, {
        title: view.name || "no-name"
      })
    );
    // eslint-disable-next-line standard/no-callback-literal
    cb && cb([...state.visitedViews]);
  },
  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.path === view.path) {
        state.visitedViews.splice(i, 1);
        break;
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name) {
        const index = state.cachedViews.indexOf(i);
        state.cachedViews.splice(index, 1);
        break;
      }
    }
  },
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = [];
  },
  DEL_ALL_VISITED_VIEWS: state => {
    state.visitedViews = [];
  },
  DEL_ALL_VIEWS: state => {
    state.visitedViews = [];
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
